//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package upgrade

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/fs_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/fs_api"
	"github.com/google/uuid"
)

type UpToVer33 struct{}

func (up *UpToVer33) Exec(ctx context.Context) error {
	exist, err := fs_dao.GlobalFsDao.ExistSwStoreFs(ctx)
	if err != nil {
		return err
	}
	if !exist {
		swStoreFsId := uuid.NewString()
		err = fs_dao.FsInfoDao.Insert(ctx, &fs_dao.FsInfoDbItem{
			FsId:      swStoreFsId,
			OwnerType: int32(fs_api.FS_OWNER_TYPE_FS_OWNER_TYPE_GLOBAL),
			OwnerId:   "",
			Status: fs_dao.FsStatusDbItem{
				FileCount:     0,
				TotalFileSize: 0,
			},
		})
		if err != nil {
			return err
		}
		err = fs_dao.GlobalFsDao.InsertSwStoreFs(ctx, swStoreFsId)
		if err != nil {
			return err
		}
	}

	return dao.VersionDao.Set(ctx, dao.VERSION_KEY_SCHEMA, 33)
}
